//
//  InspectController.m
//  LuCaiZhu
//
//  Created by 邓龙 on 2019/4/17.
//

#import "InspectController.h"
#import "HandleNetProduct+Transaction.h"
#import "FitterViewController.h"
#import "RansomDetailController.h"
#import "SubscribeController.h"
#import "CKPuslichTool.h"
#import "MJRefresh.h"
#import "UIColor+NN.h"
#import "UserModel.h"
#import "InspectCell.h"

@interface InspectController ()<UITableViewDelegate, UITableViewDataSource>
{
    int count;
}

@property (weak, nonatomic) IBOutlet UITableView *inspectTable;
@property (weak, nonatomic) IBOutlet UIView *selectView;
@property (weak, nonatomic) IBOutlet UILabel *selectLab;
@property (weak, nonatomic) IBOutlet UIView *noDataView;
@property (weak, nonatomic) IBOutlet UITableView *selectTable;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (nonatomic, copy) NSString *IsDropDown;

@property(nonatomic,strong)NSString *transType;
@property(nonatomic,strong)NSString *transTime;
@property (nonatomic, copy) NSString *IsDsq;
@property (nonatomic, copy) NSString *selectId;

@property (nonatomic, strong) FitterViewController *myFitter;
@property (nonatomic, strong) NSMutableArray *records;
@property (nonatomic, strong) NSArray *productList;

@end

@implementation InspectController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"投资检视";
    
    _selectView.layer.borderWidth = 0.5;
    _selectView.layer.borderColor = [UIColor colorWithHexString:@"#ececec"].CGColor;
    _selectView.layer.cornerRadius = 2;
    
    _inspectTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _selectTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    _myFitter=(FitterViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"FitterViewController"];
    _myFitter.isTouZhi = YES;
    
    self.noDataView.hidden = YES;
    _selectTable.hidden = YES;
    _bgView.hidden = YES;
    _selectLab.text = @"";
    _topConstraint.constant = 0;
    _topView.hidden = YES;
    count = 1;
    [self getInspectListAction];
    [self getXialaList];
    
    _inspectTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        count=1;
        [self getInspectListAction];
    }];
    _inspectTable.mj_footer =[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        count++;
        [self getInspectListAction];
    }];
}

- (NSMutableArray *)records {
    if (!_records) {
        _records = [NSMutableArray array];
    }
    return _records;
}

- (void)getXialaList {
    NSDictionary *param = @{@"ProductId": _productId,
                            @"CustomerId": _customerID};
    [HandleNetProduct getXialaInspectList: param Success:^(NSDictionary * _Nonnull data) {
        _IsDropDown = data[@"IsDropDown"];
        if ([_IsDropDown integerValue] == 0) {
            _topConstraint.constant = 0;
            _topView.hidden = YES;
        }
        else {
            _topConstraint.constant = 50;
            _topView.hidden = NO;
        }
        _productList = data[@"ProductTitle"];
        if (_productList.count > 0) {
            NSDictionary *productDic = _productList.firstObject;
            _selectLab.text = productDic[@"ProductName"];
            if ([productDic[@"ProductName"] containsString:@"淡水泉"]) {
                _IsDsq = @"1";
                _selectId = productDic[@"ProductId"];
                [self getInspectListAction];
            }
            else {
                _IsDsq = @"0";
            }
        }
        [_inspectTable reloadData];
    } Failure:^(NSString * _Nonnull errorInfo) {
        
    }];
}

//获取投资检视列表
- (void)getInspectListAction {
    NSDictionary *param = @{@"SelectId": _selectId?_selectId:_productId,
                            @"ProductId": _productId,
                            @"CustomerId": _customerID,
                            @"TransactionTime": _transTime?_transTime:@"",
                            @"TransactionType": _transType?_transType:@"",
                            @"PageIndex": @(count)};
    [HandleNetProduct getInspectList:param Success:^(NSDictionary * _Nonnull data) {
        NSArray *list = data[@"Records"];
        if (count == 1) {
            [self.records removeAllObjects];
        }
        if (list.count > 0) {
            [self.records addObjectsFromArray:list];
        }
        if (_records.count == 0) {
            self.noDataView.hidden = NO;
        }
        else {
            self.noDataView.hidden = YES;
        }
        [_inspectTable reloadData];
        
        if (_records.count < 20) {
            _inspectTable.mj_footer.hidden = YES;
        }
        
        [_inspectTable.mj_header endRefreshing];
        [_inspectTable.mj_footer endRefreshing];
    } Failure:^(NSString * _Nonnull errorInfo) {
        [_inspectTable.mj_header endRefreshing];
        [_inspectTable.mj_footer endRefreshing];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([tableView isEqual:_selectTable]) {
        return 1;
    }
    else {
        return _records.count;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([tableView isEqual:_selectTable]) {
        return _productList.count;
    }
    else {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:_selectTable]) {
        return 45;
    }
    else {
        return 60;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if ([tableView isEqual:_selectTable]) {
        return 0;
    }
    else {
        return 10;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10)];
    footView.backgroundColor = [UIColor clearColor];
    return footView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:_selectTable]) {
        InspectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InspectCell1"];
        [cell setSelectData:_productList[indexPath.row]];
        return cell;
    }
    else {
        InspectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InspectCell"];
        [cell setInspectData:_records[indexPath.section]];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:_selectTable]) {
        _selectTable.hidden = YES;
        _bgView.hidden = YES;
        NSDictionary *productDic = _productList[indexPath.row];
        _selectId = productDic[@"ProductId"];
        _selectLab.text = productDic[@"ProductName"];
        [self getInspectListAction];
    }
    else {
        NSDictionary *recordDic = _records[indexPath.section];
        if ([recordDic[@"TransactionType"] isEqualToString:@"认购"] || [recordDic[@"TransactionType"] isEqualToString:@"追加认购"]) {
            SubscribeController *vc=(SubscribeController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"SubscribeController"];
            NSDictionary *dic = _records[indexPath.section];
            vc.redeemId = dic[@"Id"];
            [self.navigationController pushViewController:vc animated:YES];
        }
        else {
            RansomDetailController *vc=(RansomDetailController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"RansomDetailController"];
            NSDictionary *dic = _records[indexPath.section];
            vc.redeemId = dic[@"Id"];
            vc.IsDq = _IsDsq;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
}

- (IBAction)clickTopBtnAction:(UIButton *)sender {
    if ([_IsDropDown integerValue] == 1) {
        _selectTable.hidden = NO;
        _bgView.hidden = NO;
        [_selectTable reloadData];
    }
}

- (IBAction)clickBottomAction:(UIButton *)sender {
    __weak  InspectController   *sSelf=self;
    _myFitter.returnFilters = ^(NSString *time, NSString *category) {
        NSLog(@"time:%@  category:%@",time,category);
        sSelf.transType=category;
        sSelf.transTime=time;
        
        [sSelf getInspectListAction];
    };
    [_myFitter setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [_myFitter setModalPresentationStyle:UIModalPresentationOverFullScreen];
    [self presentViewController:_myFitter animated:YES completion:nil];
}

@end
